package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/6 17:26
 * @description：https://leetcode-cn.com/problems/simplify-path/
 */
public class 简化路径 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.simplifyPath("/home/"));
    }

    class Solution {
        public String simplifyPath(String path) {
            String[] strings = path.split("/");
            Deque<String> stack = new ArrayDeque<>();
            for (String string : strings) {
                if (string.equals("") || string.equals(".")) continue;
                if (string.equals("..")) {
                    if (!stack.isEmpty())
                        stack.pop();
                } else {
                    stack.push(string);
                }
            }
            if (stack.isEmpty()) return "/";
            StringBuilder builder = new StringBuilder();
            while (!stack.isEmpty()) {
                builder.append("/");
                builder.append(stack.pollLast());
            }
            return builder.toString();
        }
    }
}
